package com.example.big_orange2.controller.WxSmallProgram;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.big_orange2.authorization.annotation.AuthIgnore;
import com.example.big_orange2.authorization.manager.TokenManager;
import com.example.big_orange2.common.Result;
import com.example.big_orange2.entity.CodeLoginKey;
import com.example.big_orange2.entity.Staff;
import com.example.big_orange2.entity.udata;
import com.example.big_orange2.entity.uiddata;
import com.example.big_orange2.mapper.StaffMapper;
import com.example.big_orange2.service.QrService;
import com.example.big_orange2.utils.QrCodeUtils;
import com.example.big_orange2.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/miniqr")
public class QrController {
    @Autowired
    QrService qrService;

    @Autowired
    private RedisUtils redisUtils;

    @Resource
    StaffMapper staffMapper;

    @Autowired
    private QrCodeUtils qrCodeUtils;

    @Autowired
    private TokenManager tokenManager;

    // 模拟数据库存储或者缓存存储
    Map<String, Staff> waitMap = new ConcurrentHashMap<>(64);

    @AuthIgnore
    @GetMapping("/getqrcode")
    public Result<?>  generateV3() throws IOException {
        uiddata uida = new uiddata();
        Result<?> result = new Result<>();
        String uid= UUID.randomUUID().toString().replace("-", "");

        redisUtils.set(uid,"-1",2, TimeUnit.HOURS);
        String content=uid;
//        qrService.generateStream(content,servletResponse);
         String ImageUrl=qrCodeUtils.crateQRCode(content,300,300);
         uida.setUid(uid);
         uida.setImageurl(ImageUrl);
         result.setCode("200");
         result.setData2(uida);
         return result;

    }

    @AuthIgnore
    @GetMapping("/checkuser")
    public  Result<?>  checkuser(@RequestBody udata udata) {

        Result<?> result = new Result<>();
        String uid=udata.getUid();
        Staff staff=udata.getStaff();
        LambdaQueryWrapper<Staff> wrapper = Wrappers.<Staff>lambdaQuery();
        wrapper.eq(Staff::getOpenid,staff.getStaffid());
        List<Staff> Loanhandle = staffMapper.selectList(wrapper);//
//        System.out.println(Loanhandle);
        if(Loanhandle.size() == 0){
//            System.out.println("为空情况，无对应用户");
            result.setResult("fail");
            result.setMsg("无此用户");
            return result;
        }else{
            String token=tokenManager.getTokenS(Loanhandle.get(0));
            result.setCode("200");
//            result.setData1(Loanhandle.get(0));
//            result.setToken(token);
            redisUtils.set(uid,"1",2, TimeUnit.HOURS);
            waitMap.put(uid,Loanhandle.get(0));
            return  result;
        }

    }

    @AuthIgnore
    @GetMapping("/updatestatus/{uuid}")
    public  Result<?>  updatestatus(@PathVariable String uuid) {
        Result<?> result = new Result<>();
        redisUtils.set(uuid,"0",2, TimeUnit.HOURS);
        result.setCode("200");
        return  result;
    }

    @AuthIgnore
    @GetMapping("/getstatus/{uuid}")
    public  Result<?>  getstatus(@PathVariable String uuid) {
        udata udata=new udata();
        Result<?> result = new Result<>();
        String status=redisUtils.get(uuid).toString();
        if(status=="1"){
            Staff staff=waitMap.get(uuid);
            result.setData1(staff);
        }
        result.success(status);
        return  result;
    }
}
